package cn.xzq.cstm.web.sevlet;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.xzq.cstm.domain.Customer;
import cn.xzq.cstm.domain.PageBean;
import cn.xzq.cstm.service.CustomerService;
import cn.xzq.utils.CommonUtils;
import cn.xzq.utils.servlet.BaseServlet;

/**
 * web层
 */
public class CustomerServlet extends BaseServlet{
	private static final long serialVersionUID = 1L;
	private static CustomerService customerService = new CustomerService();
	
	
	/**
	 * 添加客户
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		/*
		 * 1、分装表单参数到Customer对象
		 * 2、补全：cid，使用UUID
		 * 3、使用service方法完成添加工作
		 * 4、向requst域中添加成功信息
		 * 5、转发到msg.jsp
		 */
		Customer customer = CommonUtils.toBean(req.getParameterMap(), Customer.class);
		customer.setCid(CommonUtils.uuid());
		customerService.add(customer);
		req.setAttribute("msg", "添加客户成功！");
		return "f:/msg.jsp";	
	}
	
	/**
	 * 查询所有客户
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	/*public String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		
		 * 1、使用service方法完成查询工作
		 * 2、保存到request域中
		 * 3、转发list.jsp
		 
		req.setAttribute("cstmlist", customerService.findAll());
		return "f:/list.jsp";
	}*/
	/**
	 * 查询所有客户-分页
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String findAll(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		/*
		 * 1、获取页面传递的currPageCode当前页面值
		 * 2、给定pageSize值
		 * 3、使用pageSize、currPageCode得到pageBean，保存到request域中
		 * 4、转发到list.jsp
		 */
		String url = getDatsUrl(req);
		int currPageCode = getCurrPageCode(req); //获取当前页码值
		int pageSize = 15;//给定每页15行记录
		PageBean<Customer> pageBean = customerService.findAll(currPageCode, pageSize);
		pageBean.setUrl(url);
		req.setAttribute("pageBean", pageBean);
		return "f:/list.jsp";
	}
	
	/**
	 * 获取当前页码值
	 * @param req
	 * @return
	 */
	public int getCurrPageCode(HttpServletRequest req){
		String value = req.getParameter("currPageCode");
		if(value == null || value.trim().isEmpty()){
			return 1;
		}else{
			return Integer.parseInt(value);
		}
	}
	
	
	/**
	 * 编辑前动作
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String preEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		/*
		 * 1、获取参数cid
		 * 2、调用service方法查询
		 * 3、保存到request域中
		 * 4、转发到edit.jsp
		 */
		String cid = req.getParameter("cid");
		req.setAttribute("cstm", customerService.findById(cid));
		return "f:/edit.jsp";
	}
	
	/**
	 * 修改客户
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String edit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		/*
		 * 1、封装表单参数到Customer对象
		 * 2、使用service方法完成添加工作
		 * 3、向requst域中添加成功信息
		 * 4、转发到msg.jsp
		 */
		Customer customer = CommonUtils.toBean(req.getParameterMap(), Customer.class);
		customerService.update(customer);
		req.setAttribute("msg", "修改客户成功！");
		return "f:/msg.jsp";
	}
	
	/**
	 * 根据id删除一个客户
	 * @param req
	 * @param resp
	 * @return
	 * @throws ServletException
	 * @throws IOException
	 */
	public String delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		/*
		 * 1、获取参数cid
		 * 2、调用service方法查询
		 * 4、转发到edit.jsp
		 */
		String cid = req.getParameter("cid");
		customerService.deleteById(cid);
		req.setAttribute("msg", "删除客户成功！");
		return "f:/msg.jsp";
	}
	
	/**
	 * 多条件查询
	 * @param req
	 * @param resp
	 * @return String 
	 * @throws ServletException
	 * @throws IOException
	 */
	/*public String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		
		 * 1、封装表单参数到Customer对象，只带（cname, gender, email, cellphone）
		 * 2、使用service方法完成添加工作
		 * 3、向requst域
		 * 4、转发到list.jsp显示查询结果
		 
		Customer customer = CommonUtils.toBean(req.getParameterMap(), Customer.class);
		req.setAttribute("cstmlist", customerService.query(customer));
		return "f:/list.jsp";
	}*/
	
	/**
	 * 多条件查询-分页
	 * @param req
	 * @param resp
	 * @return String 
	 * @throws ServletException
	 * @throws IOException
-	 */
	public String query(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{
		/* 
		 * 0、获取条件数据，封装到Customer对象中
		 * 1、获取页面传递的currPageCode当前页面值
		 * 2、给定pageSize值
		 * 3、使用pageSize、currPageCode得到pageBean，保存到request域中
		 * 4、转发到list.jsp
		 */
		//获取url,处理get请求方式的编码问题
		String url = getDatsUrl(req);
		//System.out.println("---->"+url);
		Customer customer = CommonUtils.toBean(req.getParameterMap(), Customer.class);
		//customer = toEncodeCustomer(customer); 非utf-8编码要转码
		int currPageCode = getCurrPageCode(req); //获取当前页码值
		int pageSize = 15;//给定每页15行记录
		PageBean<Customer> pageBean = customerService.query(customer, currPageCode, pageSize);
		pageBean.setUrl(url);//将url保存，方便前台页面使用
		req.setAttribute("pageBean", pageBean);
		return "f:/list.jsp";
	}
	
	private String getDatsUrl(HttpServletRequest req){
		String contextPath = req.getContextPath();
		String servletPath = req.getServletPath();
		String queryString = req.getQueryString();
		
		//去除currPageCode参数
		if(queryString.contains("currPageCode")){
			int indexOf = queryString.lastIndexOf("&currPageCode=");
			queryString = queryString.substring(0, indexOf);		
		}
		return contextPath+servletPath+"?"+queryString;
	}
	
	//处理get请求的bean对象
	public Customer toEncodeCustomer(Customer customer) throws UnsupportedEncodingException{
		String cname = customer.getCname();
		String gender = customer.getGender();
		String cellphone = customer.getCellphone();
		String email = customer.getEmail();
		
		if(!CommonUtils.isRealNull(cname)){
			cname = new String(cname.getBytes("ISO-8859-1"), "UTF-8");
			customer.setCname(cname);
		}
		if(!CommonUtils.isRealNull(gender)){
			System.out.println(gender);
			gender = new String(gender.getBytes("UTF-8"), "UTF-8");
			customer.setGender(gender);
		}
		if(!CommonUtils.isRealNull(cellphone)){
			cellphone = new String(cellphone.getBytes("ISO-8859-1"), "UTF-8");
			customer.setCellphone(cellphone);
		}
		if(!CommonUtils.isRealNull(email)){
			email = new String(email.getBytes("ISO-8859-1"), "UTF-8");
			customer.setEmail(email);
		}		
		
		return customer;
		
	}

}
